using System;

namespace RapidHDL
{
	/// <summary>
	/// Summary description for Simulation.
	/// </summary>
	public class Simulation
	{
		public RapidHardware RapidHardware;
		public Clock Clock;

        public System.Collections.ArrayList ComponentTests;

		public Simulation(RapidHardware poRapidHardware)
		{
			RapidHardware = poRapidHardware;
			Clock = new Clock(poRapidHardware);
			Clock.Reset();
            Clock.ScheduleClockEvent(1, this.ResetSimulationEvent);
            ComponentTests = new System.Collections.ArrayList();
		}

        public void SetComponentTrace(string psPath, string psLabel)
        {
            Component oComponent = RapidHardware.Structure.GetComponent(psPath);
            oComponent.SetTrace(psLabel);
        }

        public void SetNodeVectorTrace(string psPath, string psLabel)
        {
            NodeVector oNodeVector = RapidHardware.Structure.GetNodeVector(psPath);
            foreach (Node oNode in oNodeVector.Nodes)
            {
                oNode.Net.SetTrace(psLabel);
            }
        }

        public void SetNodeVectorTrace(string psPath,int piBit, string psLabel)
        {
            NodeVector oNodeVector = RapidHardware.Structure.GetNodeVector(psPath);
            Node oNode = (Node)oNodeVector.Nodes[piBit];
            oNode.Net.SetTrace(psLabel);
        }

        public void AddNetTrace(string psPath, string psLabel)
        {
            Component oComponent = RapidHardware.Structure.GetComponent(psPath);
            oComponent.SetTrace(psLabel);
        }

        public void ResetSimulationEvent(double timeNow)
        {
            //System.Diagnostics.Debug.Assert(timeNow == 1);
            System.Diagnostics.Debug.Assert(RapidHardware.Structure.TopLevelComponent.BaseInitializeSimulation());
        }

        public void AddComponentTest(string psTestName, string psComponentPath)
        {
            ComponentTestParameter oTestParameter = new ComponentTestParameter();
            oTestParameter.ComponentPath = psComponentPath;
            oTestParameter.TestName = psTestName;
            ComponentTests.Add(oTestParameter);
        }

        public string TestComponents()
        {
            string sResult = "";
            string sCombinedResult = "";
            foreach(ComponentTestParameter oTestParameter in ComponentTests)
            {
                if (oTestParameter.ComponentPath.ToUpper() == "ALL")
                {
                    sResult = RapidHardware.Structure.TestAll(oTestParameter.TestName);
                }
                else
                {
                    sResult = RapidHardware.Structure.TestComponent(oTestParameter.TestName, oTestParameter.ComponentPath);
                }
                sCombinedResult += sResult + "\n";
            }
            return sCombinedResult;
        }		
	}
}
